require"common"

--============================================判断字符串==============================================
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )

        for i=1,len do
                c = string.sub(num,i,i)
                if not (tonumber(c) == 0  or tonumber(c) == 1 or tonumber(c) == 3 ) then return nil end
        end

        return true
end

function Compute(pnInput, nTotal, nSelect)
	local i
	local result = 0
	local temp = {}

	if nTotal < nSelect then
		return 0
	elseif nSelect == 1 then
		for i = 1, nTotal do
			result = result + pnInput[i]
		end
	elseif nTotal == nSelect then
		result = 1
		for i = 1, nTotal do
			result = result * pnInput[i]
		end
	elseif nTotal > nSelect then
		for i = 2, #pnInput do
			temp[i - 1] = pnInput[i]
		end
		result = pnInput[1] * Compute(temp,nTotal - 1, nSelect - 1) + Compute(temp,nTotal - 1, nSelect)
	end

	return result
end

function ComputeDTBingNum(jItemstr,djStr,n)
	local queryStr = {}    ---查询指令串
	local bullStr = {}		----开奖指令串
	local bingoNum = 0

	for i=1,#jItemstr do
		jitemLen = string.len(jItemstr[i])
		queryStr[i]={}
		for j=1,jitemLen, n do
			if n == 1 then
				queryStr[i][j] = string.sub(jItemstr[i],j,j)
			else
				queryStr[i][(j+1)/2] = string.sub(jItemstr[i],j,j+1)
			end
		end
	end

	for i=1, string.len(djStr), n do
		if n == 1 then
			bullStr[i] = string.sub(djStr,i,i)
		else
			bullStr[(i+1)/2] = string.sub(djStr,i,i+1)
		end
	end

	for i=1,14 do
			for _,k in ipairs(queryStr[i]) do
				if k == bullStr[i]  then
					bingoNum = bingoNum + 1
				end
			end
		end


	return bingoNum

end

--================================返回注数,倍投数 , 投注串, 指令串================================
function random_rx9( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  1402#注码#B倍数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = selectNumber(14,3,1,0)
			local xpos = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14)       --*的位置
			for i=1,5 do
				for j=1,14 do
					if j== tonumber(xpos[i]) then
						nums[j] = "*"
					end

				end
			end
			for i=1,14 do
				zlStr = zlStr..nums[i]
			end

			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_rx9('1', zlStr)
	end

	return butNum, butPower, butStr, zlStr


end

--==============================返回注数,倍投数,投注串=====================================
function check_rx9( butType, inStr)
	local qxStr;
	local qjStr;
	local qjbStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	local dNum = 0;
	local tNum = 0;
	local dhItem = {}
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjbStr = string.gsub( inStr, "#B", "0")
	qjStr = string.gsub( qjbStr, "#", "0")
	qxStr = string.gsub( qjStr, "*", "0")
	qdhStr = string.gsub( qxStr, ",", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qdhStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 901#注码#B倍数  901#310301**111***#*1*301**1110*0
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 14 then return 0 end
			cxStr = string.gsub(xItem[i],"*","")
			if string.len(cxStr) ~= 9 then return 0 end
			if not judge_str(cxStr) then return 0 end
		end
	--复式自选 903#A#B#C#D#E#F#G#H#I#J#K#L#M#N  903#31#310#*#10#30#*#*#*#*30#31#30#31#31
	elseif butType == '2' then
		pos = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
		bitNum = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}
		xItem = Split(itemList[1], "#")
		if #xItem ~= 14 then return 0 end
		for i = 1, 14 do
			if string.find(xItem[i], "%d") then
				bitNum[i] = string.len(xItem[i])
				pos[i] = 1
			end
		end


		butNum = 1
		j = 0         --*的个数
		for i=1,14 do
			if pos[i] == 1 then
				if string.len(xItem[i]) > 3 then return 0 end
				if not judge_str(xItem[i])  then return 0 end
				butNum = butNum*bitNum[i]
				j = j + 1
			end
		end

		if j ~= 9 then return 0 end
	elseif butType == '3' then
		local szIput = {}
		xItem = Split(itemList[1], "#")
		if(#xItem ~= 2) then return 0 end
		dhItem[1] = {}
		dhItem[2] = {}
		dhItem[1] = Split(xItem[1], ",")
		dhItem[2] = Split(xItem[2], ",")
		if #dhItem[1] ~= 14  or #dhItem[2] ~= 14 then return 0 end
		j = 1
		butNum = 1
		for i=1,14 do
			if dhItem[1][i] ~= '*' then
				dNum = dNum + 1
				butNum = butNum * string.len(dhItem[1][i])
			end
			if dhItem[2][i] ~= '*' then
				tNum = tNum + 1
				szIput[j] = string.len(dhItem[2][i])
				j = j + 1
			end
		end
		if tNum + dNum < 9 or tNum + dNum > 14 then return 0 end
		num = Compute(szIput, tNum, 9 - dNum)
		butNum = butNum * Compute(szIput, tNum, 9 - dNum)

	else
		return 0
	end;
	return butNum,butPower,inStr,inStr
end

--============================查询==========================================================
function query_rx9(butType,queryStr,djStr)
	local butPower = 1
	local itemList = {}
	local result = 0
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	if  butType == '1' then
		jItem = Split(itemList[1], "#")
		result =MultSimpCompute(jItem,djStr,butPower,14,1,butType)

	elseif butType == '2' then
		jItem = Split(itemList[1], "#")
		result = MultSimpCompute(jItem,djStr,butPower,14,1,butType)

	elseif butType == '3' then
		jItem = Split(itemList[1], "#")
		dItem = Split(jItem[1], ",")
		tItem = Split(jItem[2], ",")
		dNum = 0
		tNum = 0
		for i=1, 14 do
			if dItem[i] ~= "*" then
				dNum = dNum + 1
			end
			if tItem[i] ~= "*" then
				tNum = tNum + 1
			end
		end
		dBingoNum = ComputeDTBingNum(dItem, djStr, 1)
		tBingoNum = ComputeDTBingNum(tItem, djStr, 1)

		if dBingoNum == dNum then
			result = combination(tBingoNum, 9 - dNum) * butPower
		else
			result = 0
		end


	else
		return 0
	end

	return result

end

--print(random_rx9('1',"1#B11"))
--print(check_rx9('1',"310301**111***#*1*301**1110*0#B10"))
--print(check_rx9('2',"310#0#0#0#0#0#0#*#0#310#*#*#*#*#B1"))
--print(check_rx9('3',"1,10,3,*,*,*,*,*,*,*,*,*,*,*#*,*,*,0,0,0,0,1,0,0,0,01,3,1"))
--print(query_rx9('3',"3,10,0,*,*,*,*,*,*,*,*,*,*,*#*,*,*,1,1,0,0,0,1,1,1,01,1,1#B10","31030111111111"))
--print(query_rx9('2',"31#310#*#310#310#*#*#*#*#310#310#310#31#31#B99","31030111111111"))
